package backend

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"miaoyuadmin/api/backend"
	"miaoyuadmin/api/backend/response"
	"miaoyuadmin/internal/model"
	"miaoyuadmin/internal/service"
	"strings"
)

var UserLevel = new(userLevelCtl)

type userLevelCtl struct {
}

func (c *userLevelCtl) Index(ctx context.Context, req *backend.UserLevelIndexReq) (res *backend.UserLevelIndexRes, err error) {
	response.BuildTpl(g.RequestFromCtx(ctx), "public/layout.html").WriteTpl(g.MapStrAny{
		"mainTpl": "user/level/index.html",
		"footTpl": "user/level/index.js.html",
		"title":   "职级管理",
	})

	return
}
func (c *userLevelCtl) Add(ctx context.Context, req *backend.UserLevelAddReq) (res *backend.UserLevelAddRes, err error) {
	response.BuildTpl(g.RequestFromCtx(ctx), "public/layout.html").WriteTpl(g.MapStrAny{
		"mainTpl":  "user/level/add.html",
		"footTpl":  "user/level/add.js.html",
		"siteName": "控制台",
	})
	return
}
func (c *userLevelCtl) Edit(ctx context.Context, req *backend.UserLevelEditReq) (res *backend.UserLevelEditRes, err error) {
	resId := req.ResId

	itemRes, resErr := service.UserLevel().GetItemById(ctx, resId)
	if resErr != nil {
		err = resErr
		response.UniResp(g.RequestFromCtx(ctx), err.Error(), "")
		return
	}
	response.BuildTpl(g.RequestFromCtx(ctx), "public/layout.html").WriteTpl(g.MapStrAny{
		"mainTpl":  "user/level/add.html",
		"footTpl":  "user/level/edit.js.html",
		"siteName": "控制台",
		"data":     itemRes,
	})
	return
}

func (c *userLevelCtl) DelDo(ctx context.Context, req *backend.UserLevelDelDoReq) (res *backend.UserLevelDelDoRes, err error) {
	resId := req.ResId
	commonJson := response.CommonResp(g.RequestFromCtx(ctx))
	_, resErr := service.UserLevel().Del(ctx, resId)
	if resErr != nil {
		err = resErr
		commonJson.SetErrorJsonExit(err.Error())
		return
	}
	commonJson.SetSuccessJsonExit("操作成功", nil)
	return

}

func (c *userLevelCtl) ListDo(ctx context.Context, req *backend.UserLevelListDoReq) (res *backend.UserLevelListDoRes, err error) {

	commonJson := response.CommonResp(g.RequestFromCtx(ctx))
	listOptions := model.UserListInput{Page: 1, Limit: 10}
	if req.Page <= 0 {
		listOptions.Page = 1
	} else {
		listOptions.Page = req.Page
	}
	if req.Limit <= 0 {
		listOptions.Limit = 10
	} else {
		listOptions.Limit = req.Limit
	}
	if req.Keywords = strings.Trim(req.Keywords, " "); req.Keywords != "" {
		listOptions.Keywords = req.Keywords
	}
	listRes, count, resErr := service.UserLevel().GetPageList(ctx, listOptions)
	if resErr != nil {
		err = resErr
		return
	}
	resData := response.JsonResult{Data: listRes, Count: count, Msg: "ok"}
	commonJson.SetSuccessMapExit(resData)

	return
}
func (c *userLevelCtl) SubmitDo(ctx context.Context, req *backend.UserLevelSubmitDoReq) (res *backend.UserLevelSubmitDoRes, err error) {
	var submitInput model.UserLevelSubmitInput
	commonJson := response.CommonResp(g.RequestFromCtx(ctx))
	err = gconv.Scan(req, &submitInput)
	if err != nil {
		return
	}
	resId := req.ResId
	if resId != 0 {
		_, err = service.UserLevel().Edit(ctx, submitInput, resId)
		if err != nil {
			commonJson.SetErrorJsonExit(err.Error())
		} else {
			commonJson.SetSuccessJsonExit("修改成功", nil)
		}
		return
	} else {
		_, err = service.UserLevel().Add(ctx, submitInput)
		if err != nil {
			commonJson.SetErrorJsonExit(err.Error())
		}

		commonJson.SetSuccessJsonExit("添加成功", nil)
	}

	return
}
